<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
  <link href="//gmpg.org/xfn/11" rel="profile">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="Hugo 0.68.3" />

  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>邮箱验证码 &middot; 我的博客</title>

  
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/print.css" media="print">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/poole.css">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/syntax.css">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/hyde.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface|PT+Sans:400,400i,700">


  
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
  <link rel="shortcut icon" href="/favicon.png">

  
  
</head>

  <body class="theme-base-0b ">
  <aside class="sidebar">
  <div class="container sidebar-sticky">
    <div class="sidebar-about">
      <a href="/my_technology_blog/"><h1>我的博客</h1></a>
      <p class="lead">
       杨博的博客 
      </p>
    </div>

    <nav>
      <ul class="sidebar-nav">
        <li><a href="/my_technology_blog/">Home</a> </li>
        
      </ul>
    </nav>

    <p>&copy; 2021. All rights reserved. </p>
  </div>
</aside>

    <main class="content container">
    <div class="post">
  <h1>邮箱验证码</h1>
  <time datetime=2020-05-12T10:11:51&#43;0800 class="post-date">Tue, May 12, 2020</time>
  <p>在setting中进行配置</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">EMAIL_USE_SSL <span style="color:#f92672">=</span> True  <span style="color:#75715e"># Secure Sockets Layer 安全套接层, 取决于邮件服务器是否开启加密协议</span>
EMAIL_HOST <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;smtp.qq.com&#39;</span>  <span style="color:#75715e"># 邮件服务器地址 ， 如果是163改成smtp.163.com</span>
EMAIL_PORT <span style="color:#f92672">=</span> <span style="color:#ae81ff">465</span>  <span style="color:#75715e"># 邮件服务器端口</span>
EMAIL_HOST_USER <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;1349157185@qq.com&#39;</span>  <span style="color:#75715e"># 登陆邮件服务器的账号</span>
EMAIL_HOST_PASSWORD <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;zdcyocfdngqfhcde&#39;</span>  <span style="color:#75715e"># 登陆邮件服务器的授权密码,要在邮箱里进行设置</span>
DEFAULT_FROM_EMAIL <span style="color:#f92672">=</span> EMAIL_HOST_USER  <span style="color:#75715e"># 邮件的发送者</span>

<span style="color:#75715e"># 允许哪些人可以请求你和请求方式</span>
CORS_ORIGIN_ALLOW_ALL <span style="color:#f92672">=</span> True  <span style="color:#75715e"># 改为true是所有人都可以访问我</span>
</code></pre></div><p>view导包</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">from</span> django.http <span style="color:#f92672">import</span> JsonResponse
<span style="color:#f92672">from</span> django.core.mail <span style="color:#f92672">import</span> send_mail  <span style="color:#75715e"># 用来发送用</span>
<span style="color:#f92672">from</span> vue001project.settings <span style="color:#f92672">import</span> EMAIL_HOST_USER    <span style="color:#75715e"># 导入setting中的配置</span>
<span style="color:#f92672">from</span> vue001project.settings <span style="color:#f92672">import</span> DEFAULT_FROM_EMAIL	<span style="color:#75715e"># 导入setting中的配置</span>
<span style="color:#f92672">import</span> redis    <span style="color:#75715e"># 导入redis，存储验证码，后期用于验证</span>
</code></pre></div><p>redis连接及配置</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#75715e">#定义ip地址和端口</span>
host <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;120.26.175.79&#34;</span>    <span style="color:#75715e"># 这个用的远程链接redis</span>
port <span style="color:#f92672">=</span> <span style="color:#ae81ff">6379</span>

<span style="color:#75715e">#链接对象</span>
r <span style="color:#f92672">=</span> redis<span style="color:#f92672">.</span>Redis(host<span style="color:#f92672">=</span>host, port<span style="color:#f92672">=</span>port, decode_responses<span style="color:#f92672">=</span>True)   <span style="color:#75715e"># 防止验证码前加“b”</span>
</code></pre></div><p>生成随机验证码</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#75715e"># 生成随机验证码</span>
<span style="color:#66d9ef">def</span> <span style="color:#a6e22e">get_random_str</span>():
    <span style="color:#75715e">#验证码是由 字母a~z  A~Z 数字 0~9 组成</span>
    <span style="color:#75715e"># 在 ascii 码中 小写a的起点是97 大写A起点是65</span>
    a_ <span style="color:#f92672">=</span> [chr(var) <span style="color:#66d9ef">for</span> var <span style="color:#f92672">in</span> range(<span style="color:#ae81ff">97</span>, <span style="color:#ae81ff">123</span>)]  <span style="color:#75715e"># 小写a的列表推导式</span>
    A_ <span style="color:#f92672">=</span> [chr(var) <span style="color:#66d9ef">for</span> var <span style="color:#f92672">in</span> range(<span style="color:#ae81ff">65</span>, <span style="color:#ae81ff">91</span>)]   <span style="color:#75715e"># 大写A的列表推导式</span>
    num_ <span style="color:#f92672">=</span> [str(var) <span style="color:#66d9ef">for</span> var <span style="color:#f92672">in</span> range(<span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">9</span>)]  <span style="color:#75715e"># 数字的</span>
    <span style="color:#75715e"># 使用sample 在列表中随机生成6个任意字母数字</span>
    <span style="color:#66d9ef">return</span> <span style="color:#e6db74">&#39;&#39;</span><span style="color:#f92672">.</span>join(random<span style="color:#f92672">.</span>sample(a_ <span style="color:#f92672">+</span> A_ <span style="color:#f92672">+</span> num_, <span style="color:#ae81ff">6</span>))
</code></pre></div><p>发送验证码以及存储redis并设置过期时间</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">EmailVerify</span>(View):
    <span style="color:#75715e"># 邮件验证码请求的接口</span>
    <span style="color:#e6db74">&#39;&#39;&#39;
</span><span style="color:#e6db74">    1.先检查 对应邮件是否在redis里已经存在了验证码
</span><span style="color:#e6db74">    2.发现没有存在，那么需要生成6位随机字符
</span><span style="color:#e6db74">    3. 存储入到redis里，并设置60s的过期时间
</span><span style="color:#e6db74">         key:value:time  ==  email:6位随机字符:60s
</span><span style="color:#e6db74">    &#39;&#39;&#39;</span>

    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">post</span>(self, request):
        email <span style="color:#f92672">=</span> request<span style="color:#f92672">.</span>POST<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;email&#39;</span>)  <span style="color:#75715e"># 接收到用户发来的邮箱</span>
        token <span style="color:#f92672">=</span> r<span style="color:#f92672">.</span>get(email)
        <span style="color:#75715e"># token = &#39;123456&#39;</span>
        res <span style="color:#f92672">=</span> {}
        <span style="color:#66d9ef">if</span> token:  <span style="color:#75715e"># 去redis 中查看 是否这个邮箱</span>
            res[<span style="color:#e6db74">&#39;code&#39;</span>] <span style="color:#f92672">=</span> <span style="color:#ae81ff">401</span>
            res[<span style="color:#e6db74">&#39;message&#39;</span>] <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;请一分钟后再次发送&#34;</span>
            <span style="color:#66d9ef">return</span> JsonResponse(res)

        <span style="color:#66d9ef">else</span>:
            <span style="color:#75715e"># 发送给</span>
            token <span style="color:#f92672">=</span> get_random_str()  <span style="color:#75715e"># 调用生成验证码函数，生成验证码</span>
            subject <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;亲！欢迎你来到实验楼哦&#39;</span>
            message <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;你的验证码是:</span><span style="color:#e6db74">%s</span><span style="color:#e6db74">&#39;</span> <span style="color:#f92672">%</span> token
            r<span style="color:#f92672">.</span>set(email, token)  <span style="color:#75715e"># 存储到redis</span>
            <span style="color:#75715e"># 设置访问周期为60秒</span>
            r<span style="color:#f92672">.</span>expire(email, <span style="color:#ae81ff">60</span>)
            send_mail(subject, message, DEFAULT_FROM_EMAIL,
                      [email])  <span style="color:#75715e"># 把邮件发过去</span>

            res[<span style="color:#e6db74">&#39;code&#39;</span>] <span style="color:#f92672">=</span> <span style="color:#ae81ff">200</span>
            res[<span style="color:#e6db74">&#39;message&#39;</span>] <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;验证码已发送&#34;</span>
            <span style="color:#66d9ef">return</span> JsonResponse(res)
</code></pre></div><p>验证并存入数据库</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">token <span style="color:#f92672">=</span> r<span style="color:#f92672">.</span>get(email)
<span style="color:#75715e"># 在数据库中取出对应的验证码</span>
<span style="color:#66d9ef">if</span> token <span style="color:#f92672">and</span> token <span style="color:#f92672">==</span> code:
    res[<span style="color:#e6db74">&#39;code&#39;</span>] <span style="color:#f92672">=</span> <span style="color:#ae81ff">200</span>
    res[<span style="color:#e6db74">&#39;message&#39;</span>] <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;注册成功&#34;</span>
    User<span style="color:#f92672">.</span>objects<span style="color:#f92672">.</span>create_user(
        username<span style="color:#f92672">=</span>username, password<span style="color:#f92672">=</span>password, email<span style="color:#f92672">=</span>email)
    <span style="color:#66d9ef">return</span> Response(res)

<span style="color:#66d9ef">else</span>:
    res[<span style="color:#e6db74">&#39;code&#39;</span>] <span style="color:#f92672">=</span> <span style="color:#ae81ff">403</span>
    res[<span style="color:#e6db74">&#39;message&#39;</span>] <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;验证码错误&#34;</span>
    <span style="color:#66d9ef">return</span> Response(res)
</code></pre></div>
</div>


    </main>

    
  </body>
</html>
